package com.ipet.app.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.clans.fab.FloatingActionMenu;
import com.ipet.app.R;
import com.ipet.app.adapter.FragmentOneAdapter;
import com.ipet.app.api.BaseCallBack;
import com.ipet.app.api.RequestManager;
import com.ipet.app.bean.HotPost;
import com.ipet.app.bean.apiRequestBean.PageRequest;
import com.ipet.app.bean.apiResponseBean.HotResponse;
import com.ipet.app.ui.base.BaseFragment;
import com.ipet.app.ui.empty.EmptyLayout;
import com.ipet.app.util.SpaceItemDecoration;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.List;

import retrofit2.Call;


/**
 * Created by jindongping on 16/3/8.
 */
public class MainOneFragment extends BaseFragment {
    int page = 1, limit = 15;
    private static FloatingActionMenu fabMenu;
    private PageRequest pageRequest = new PageRequest(page + "", limit + "");
    private FragmentOneAdapter adapter;

    public static MainOneFragment newInstance(FloatingActionMenu floatingActionButton) {
        MainOneFragment fragment = new MainOneFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        fabMenu = floatingActionButton;
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, null);
        initEasyRecyclerView(view);
        return view;
    }

    private void initEasyRecyclerView(final View view) {
        final EmptyLayout emptyLayout =(EmptyLayout)view.findViewById(R.id.empty_layout);
        final EasyRecyclerView recyclerView = (EasyRecyclerView) view.findViewById(R.id.easyRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.getSwipeToRefresh().setColorSchemeResources(R.color.pink_500);
        recyclerView.addItemDecoration(new SpaceItemDecoration(10));
        //设置适配器
        adapter = new FragmentOneAdapter(getContext());
        adapter.setNoMore(R.layout.view_nomore);
        recyclerView.setAdapter(adapter);
        //设置下拉刷新监听
        recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                pageRequest.setPage(page + "");
                //下拉加载数据
                RequestManager.getApi().getHotPost(pageRequest).enqueue(new BaseCallBack<HotResponse>(fabMenu, recyclerView.getSwipeToRefresh(),emptyLayout) {
                    @Override
                    public void onRequestSuccess(HotResponse response) {
                        //下拉刷新 恢复page
                        adapter.clear();
                        adapter.addAll(response.getContentList());
                    }

                    @Override
                    public void onFailure(Call<HotResponse> call, Throwable t) {
                        super.onFailure(call, t);
                        adapter.clear();
                    }
                });
            }
        });

        //刚进界面 请求一次数据 成功后隐藏emptyView 给adapter喂数据
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                recyclerView.setRefreshing(true);
                //刚进界面 请求一次数据
                RequestManager.getApi().getHotPost(pageRequest).enqueue(new BaseCallBack<HotResponse>(fabMenu, recyclerView.getSwipeToRefresh(),emptyLayout) {
                    @Override
                    public void onRequestSuccess(HotResponse response) {
                        List<HotPost> hotPosts = response.getContentList();
                        if (hotPosts.size() == 0) {
                            return;
                        }
                        adapter.addAll(hotPosts);
                    }

                });

            }
        });
        adapter.setMore(R.layout.view_more, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                pageRequest.setPage(++page + "");
                RequestManager.getApi().getHotPost(pageRequest).enqueue(new BaseCallBack<HotResponse>(fabMenu, recyclerView.getSwipeToRefresh()) {
                    @Override
                    public void onRequestSuccess(HotResponse response) {
                        List<HotPost> hotPosts = response.getContentList();
                        adapter.addAll(hotPosts);
                    }
                });

            }
        });

        adapter.setError(R.layout.view_error).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.resumeMore();
            }
        });

        //设置FloatingActionMenu 下拉隐藏
        recyclerView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0) {
                    fabMenu.hideMenu(true);
                } else if (dy < 0) {
                    fabMenu.showMenu(true);
                }
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }


}
